package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Claimresult;
import cn.wolfcode.crm.mapper.AccidentMapper;
import cn.wolfcode.crm.mapper.ClaimresultMapper;
import cn.wolfcode.crm.query.ClaimresultObjectQuery;
import cn.wolfcode.crm.query.ObjectQuery;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.IClaimresultService;
import org.apache.ibatis.binding.BindingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;


@Service
public class ClaimresultServiceImpl implements IClaimresultService {

    @Autowired
    private ClaimresultMapper claimresultMapper;
    @Autowired
    private AccidentMapper accidentMapper;

    @Override
    public void save(Claimresult claimresult) {
        Long insurerId = claimresult.getInsurer().getId();
        Long billId = claimresult.getInsurancebill().getId();
        accidentMapper.saveAccidentAndBillRelation(insurerId, billId);
        claimresultMapper.deleteByInsurerId(insurerId);
        claimresultMapper.insert(claimresult);
    }

    @Override
    public Claimresult get(Long id) {
        return claimresultMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Claimresult> getAll() {
        return claimresultMapper.selectAll();
    }

    @Override
    public PageResult<Claimresult> query(ClaimresultObjectQuery qo) {
        int totalCount = claimresultMapper.queryForCount(qo);
        if (totalCount == 0) {
            return new PageResult<>(totalCount, Collections.<Claimresult>emptyList());
        }
        return new PageResult<>(totalCount, claimresultMapper.queryForList(qo));
    }

    @Override
    public Claimresult getDetailByInsurerId(Long insurerId) {
        return claimresultMapper.getDetailByInsurerId(insurerId);
    }

    @Override
    public List<Map<String, Object>> list(ObjectQuery qo) {
        return claimresultMapper.list(qo);
    }

    public int selectClaimResultAccount() {
        int resultAccount = 0;
        try {
            resultAccount = claimresultMapper.selectClaimResultAccount();
        } catch (BindingException e) {
            e.printStackTrace();
        }
        return resultAccount;
    }
}

